package cn.wbnyua.resume.api.api2;

import cn.wbnyua.resume.core.exception.RuntimeExceptionHandler;
import cn.wbnyua.resume.mvc.domain.Education;
import cn.wbnyua.resume.mvc.service.v2.EducationService;
import cn.wbnyua.resume.util.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者：wbnyua.
 * 创建时间: 2023/2/7 14:15.
 */
@RestController
@RequestMapping("/api/education")
public class EducationApi {
    @Autowired
    private EducationService service;

    @GetMapping
    public JsonResponse<List<Education>> list(){
        return new JsonResponse<>(service.findAll());
    }

    @GetMapping("/{id}")
    public JsonResponse<Education> findById(@PathVariable Long id){
        Education education = service.findById(id);
        return new JsonResponse<>(education);
    }

    @PostMapping
    public JsonResponse<String> add(Education education){
        service.add(education);
        return JsonResponse.success();
    }

    @PutMapping
    public JsonResponse<String> update(Education education){
        System.out.println(education);
        if (education.getId()==null){
            throw new RuntimeExceptionHandler("用户数据有误");
        }
        service.update(education);
        return JsonResponse.success();
    }


    @DeleteMapping("/{id}")
    public JsonResponse<String> remove(@PathVariable Long id){
        if (id==null){
            throw new RuntimeExceptionHandler("请输入删除的编号");
        }
        service.remove(id);
        return JsonResponse.success();
    }
}
